/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editpolicies.ComponentEditPolicy;

import com.cea.papyrus.core.actions.delete.DeleteFromDiagramAction;
import com.cea.papyrus.core.actions.delete.DeleteFromModelAction;
import com.cea.papyrus.core.commands.factories.DiagramElementDeleteCommandFactory;
import com.cea.papyrus.core.commands.factories.UMLDi2ElementDeleteFactory;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;


/**
 * 
 */
public class GraphNodeWithSemanticEditPolicy extends ComponentEditPolicy {

	/**
	 * 
	 */
	protected ModelManager modelManager;

    /**
     * Return the delete factory used to return a delete command.
     * This method can be subclassed in order to return a specific factory.
      * @return
     */
    protected UMLDi2ElementDeleteFactory getDeleteCommandFactory() {
        return UMLDi2ElementDeleteFactory.eINSTANCE;
    }

	/**
	 * 
	 * 
	 * @param modelManager 
	 */
	public GraphNodeWithSemanticEditPolicy(ModelManager modelManager) {
		super();
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	protected GraphNode getGraphNode() {
		return (GraphNode)getHost().getModel();
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	protected GraphNode getParentGraphNode() {
		return (GraphNode)getHost().getParent().getModel();
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	protected GraphNodeWithSemanticEditPart getGraphNodeEditPart() {
		return (GraphNodeWithSemanticEditPart)getHost();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editpolicies.ComponentEditPolicy#getCommand(org.eclipse.gef.Request)
	 */
	/**
	 * 
	 * 
	 * @param request 
	 * 
	 * @return 
	 */
	@Override
	public Command getCommand(Request request) {
		
		if(DeleteFromDiagramAction.DELETE_FROM_DIAGRAM_ACTION_REQUEST.equals(request.getType())) {
			return getDeleteFromDiagramCommand(getParentGraphNode(), getGraphNode());
		}
		if(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_REQUEST.equals(request.getType())) {
			return getDeleteFromModelCommand();
		}
		return super.getCommand(request);
	}

    /**
     * Return the appropriate delete from model command
     * @return
     */
    protected Command getDeleteFromModelCommand() {
        if( getGraphNodeEditPart().isElementImport()){
        	return getDeleteCommandFactory().createCommand(getGraphNodeEditPart().getElementImport());
        } else {
        	return getDeleteCommandFactory().createCommand(getGraphNodeEditPart().getUmlElement());
        }
    }

	
	
	/**
	 * 
	 * 
	 * @param child 
	 * @param parent 
	 * 
	 * @return 
	 */
	protected Command getDeleteFromDiagramCommand(GraphElement parent, GraphNode child){

		CompoundCommand command = new CompoundCommand();
		if((child == null) || (parent == null)) {
			throw new IllegalArgumentException("child or parent is null ");
		}
		command.add(DiagramElementDeleteCommandFactory.eINSTANCE.createCommand((GraphElement)child));

		return command;
	}
	
}
